package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-07 22:06
 **/
@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    @RequestMapping("")
    @PermissionName("部门主页")
    @RequiresPermissions("department:index")
    public String index() {
        return "department/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("department:list")
    @PermissionName("部门数据")
    public PageResult list(QueryObject qo) {
        PageResult result = service.query(qo);
        return result;
    }

    @RequestMapping("queryDepts")
    @ResponseBody
    public List<Department> queryDepts(QueryObject qo) {
        return service.list();
    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("department:save")
    @PermissionName("部门保存")
    public JsonResult save(Department entity) {
        JsonResult result = new JsonResult();
        try {
            service.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("department:update")
    @PermissionName("部门更新")
    public JsonResult update(Department entity) {
        JsonResult result = new JsonResult();
        try {
            service.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions("department:changeState")
    @PermissionName("部门状态修改")
    public JsonResult changeState(Long id) {
        JsonResult result = new JsonResult();
        try {
            service.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }
}
